package org.kommii.encryption;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Handler {
	
	public static String encrypt(String str) {  
        String re_md5 = null;  
        try {  
            MessageDigest md5 = MessageDigest.getInstance("MD5");  
            md5.update(str.getBytes());
            byte b[] = md5.digest();
            int i; 
            StringBuffer buf = new StringBuffer(""); 
            for (int offset = 0; offset < b.length; offset++) {  
                i = b[offset];  
                if (i < 0)  
                    i += 256;  
                if (i < 16)  
                    buf.append("0");  
                buf.append(Integer.toHexString(i)); 
            }  
            re_md5 = buf.toString();
            return re_md5; 
        } catch (NoSuchAlgorithmException e) {  
            e.printStackTrace();
            return null;
        }  
         
    }  
	
	public static String encrypt2(String str) {  
        try {  
            MessageDigest md5 = MessageDigest.getInstance("MD5");  
            md5.update(str.getBytes());
            byte b[] = md5.digest();
            return new String(b); 
        } catch (NoSuchAlgorithmException e) {  
            e.printStackTrace();
            return null;
        }  
    }  
	
	public static void main(String[] args){
		//String s = Base64Helper.encode("abcdefg".getBytes());
		//System.out.println(s.length());
		/*String t = encrypt2("123456");
		System.out.println(t);
		System.out.println(t.length());
		System.out.println(Base64Helper.encode(t.getBytes()));
		long tt = System.currentTimeMillis();*/
		
		/*String t2 = encrypt("abcdefgfbffg545rgghfhtrhrhertyjhthfdghfh地方和东方红太难听gfdgf");
		System.out.println(t2);
		long tt2 = System.currentTimeMillis();
		System.out.println("time millis: "+(tt2-tt));
		System.out.println(Integer.toHexString(15));*/
		//fe76b81a9505139cb76384651f7336a9
		//fe76b81a95 5139cb76384651f7336a9
	}

}
